<?php
/*
  $Id: account_activate.php,v 0.1 2007/06/14 22:09:05 $

  Contribution for osCommerce
  http://www.oscommerce.com

  by kocu
  http://kocu.christian.net

  Released under the GNU General Public License
*/

  require('includes/application_top.php');
  
  if (ACTIVATION_CODE == 'off') tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));

// needs to be included earlier to set the success message in the messageStack
  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CREATE_ACCOUNT);

	$activation_code = '';
	if( isset($HTTP_GET_VARS['activation_id']) or (isset($HTTP_POST_VARS['activation_id'])) ) {
	    if(isset($HTTP_GET_VARS['activation_id'])) $activation_id = tep_db_prepare_input($HTTP_GET_VARS['activation_id']);
	    if(isset($HTTP_POST_VARS['activation_id'])) $activation_id = tep_db_prepare_input($HTTP_POST_VARS['activation_id']);
	    $activation_code = tep_db_prepare_input($HTTP_GET_VARS['activation_code']);
	} else {
    $activation_id = '';
	$error = true;
	$messageStack->add('account_activate', ACTIVATION_ID_ERROR);
  }

  $process = false;

  if ((isset($HTTP_POST_VARS['action']) && ($HTTP_POST_VARS['action'] == 'activate')) or (isset($HTTP_GET_VARS['submit']) && ($HTTP_GET_VARS['submit'] == 'true'))) {

    $process = true;

    if(isset($HTTP_GET_VARS['activation_code'])) $activation_code = tep_db_prepare_input($HTTP_GET_VARS['activation_code']);
    if(isset($HTTP_POST_VARS['activation_code'])) $activation_code = tep_db_prepare_input($HTTP_POST_VARS['activation_code']);
	
    $error = false;

    $check_code_query = tep_db_query("select customers_id,customers_activation_code,customers_firstname,customers_active_status,customers_default_address_id,count(*) as total from " . TABLE_CUSTOMERS . " where md5(customers_email_address) = '" . tep_db_input($activation_id) . "' group by customers_id,customers_activation_code,customers_firstname,customers_default_address_id");
	$check_code = tep_db_fetch_array($check_code_query);
	
    if ($check_code['total'] > 1) {
      $error = true;

      $messageStack->add('account_activate', ACTIVATION_ID_ERROR);
    } else {
	  if($check_code['customers_active_status'] == '1') {
		$error = true;
		
		$messageStack->add('account_activate', ACTIVATION_STATUS_ERROR);
	  } else {
		if($check_code['customers_activation_code'] <> $activation_code) {
			$error = true;
			
			$messageStack->add('account_activate', ACTIVATION_CODE_ERROR);
		} else {
			$customer_id = $check_code['customers_id'];
			$customer_first_name = $check_code['customers_firstname'];
			$customer_default_address_id = $check_code['customers_default_address_id'];
		}
	  }
	}
	
    if ($error == false) {

      tep_db_query("update " . TABLE_CUSTOMERS . " set customers_active_status = '1' where customers_id = '" . (int)$customer_id . "'");
	  
	  $customers_query = tep_db_query("select * from " . TABLE_ADDRESS_BOOK . " where customers_id = '". (int)$customer_id. "' and address_book_id = '" . (int)$customer_default_address_id . "'");
	  $customers_data = tep_db_fetch_array($customers_query);

      if (SESSION_RECREATE == 'True') {
        tep_session_recreate();
      }
	  
	  $customer_country_id = $customers_data['customers_country_id'];
	  $customer_zone_id = $customers_data['zone_id'];
	  tep_session_register('customer_id');
	  tep_session_register('customer_first_name');
	  tep_session_register('customer_default_address_id');
	  tep_session_register('customer_country_id');
	  tep_session_register('customer_zone_id');
	  
      tep_redirect(tep_href_link(FILENAME_ACCOUNT_ACTIVATE_SUCCESS, '', 'SSL'));
	  
    }
  }

  $breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_ACCOUNT_ACTIVATE, '', 'SSL'));
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>

<?php
// BOF: Header Tag Controller v2.6.3
if ( file_exists(DIR_WS_INCLUDES . 'header_tags.php') ) {
  require(DIR_WS_INCLUDES . 'header_tags.php');
} else {
?> 
  <title><?php echo TITLE; ?></title>
<?php
}
// EOF: Header Tag Controller v2.6.3
?>

<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<script language="javascript"><!--
var submitted = false;
var error = false;
var error_message = "";

function check_input(field_name, field_size, message) {
  if (form.elements[field_name] && (form.elements[field_name].type != "hidden")) {
    var field_value = form.elements[field_name].value;

    if (field_value == '' || field_value.length < field_size) {
      error_message = error_message + "* " + message + "\n";
      error = true;
    }
  }
}

function check_account_activate() {

  if (submitted == true) {
    alert("<?php echo JS_ERROR_SUBMITTED; ?>");
    return false;
  }
 
  check_input("activation_code", 1, "<?php echo ENTRY_ACTIVATION_CODE_ERROR; ?>");
 
  if (error == true) {
    alert(error_message);
    return false;
  } else {
    submitted = true;
    return true;
  }
}
  
--></script>
<!-- body //-->
<table border="0" width="100%" cellspacing="3" cellpadding="3">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="0" cellpadding="2">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><?php echo tep_draw_form('account_activate', tep_href_link(FILENAME_ACCOUNT_ACTIVATE, '', 'SSL'), 'post', 'onSubmit="return check_account_activate();"') . tep_draw_hidden_field('action', 'activate'); ?><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td class="pageHeading" align="right"><?php echo tep_image(DIR_WS_IMAGES . 'table_background_account.gif', HEADING_TITLE, HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
<?php
  if ($messageStack->size('account_activate') > 0) {
?>
      <tr>
        <td class="main"><?php echo $messageStack->output('account_activate'); ?></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
<?php
  }
?>
<?php
  if (sizeof($navigation->snapshot) > 0) {
	if($navigation->snapshot['page'] == 'create_account.php') {
?>
      <tr>
        <td><?php echo TEXT_ACCOUNT_CREATED_ACTIVATION_CODE; ?></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
<?php
	}
  }  
?>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="2">
          <tr>
            <td class="main"><?php echo CONFIRMATION_CODE; ?></td>
            <td class="inputRequirement" align="right">&nbsp;<?php echo tep_draw_hidden_field('activation_id', $activation_id); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td class="main"><br><?php echo sprintf(TEXT_ORIGIN_LOGIN, tep_href_link(FILENAME_LOGIN, tep_get_all_get_params(), 'SSL')); ?></td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="1" cellpadding="2" class="infoBox">
          <tr class="infoBoxContents">
            <td><table border="0" cellspacing="2" cellpadding="2">
			  <tr>
                <td class="main"><?php echo ENTER_CODE . tep_draw_input_field('activation_code', $activation_code); ?></td>
                <td class="main"><?php echo tep_image_submit('button_continue.gif', IMAGE_BUTTON_CONTINUE); ?></td>
              </tr>
			</table></td>
		  </tr>
        </table></td>
      </tr>
    </table></form></td>
<!-- body_text_eof //-->
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="0" cellpadding="2">
<!-- right_navigation //-->
<?php include(DIR_WS_INCLUDES . 'column_right.php'); ?>
<!-- right_navigation_eof //-->
    </table></td>
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php include(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>

<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
